<template>
  <div class="index-container">
    <div class="main-header clearfix">
      <el-row>
       <el-col :xs="8" :sm="8" :md="8" :lg="8" :xl="8">
          <el-button type="primary"  icon="el-icon-plus" class="sureBtn"  size="medium"  @click="handleAdd" > 新 增 图 标</el-button>
        </el-col>
        <el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="16">
          <div class="search fr">
            <div>
              <!-- <el-input  v-model="selectForm.name"   size="medium"  placeholder="站点名称"></el-input> -->
              <el-select
                v-model="selectForm.type"
                clearable
                placeholder="任务类型"
                filterable
                size="medium"
                style="width: 100%;"
              >
                <el-option
                  v-for="item in typeList"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id">
                </el-option>
              </el-select>
            </div>
            <el-button size="medium"   type="primary"   icon="el-icon-search"  class="fr"    @click="handleSearch" > 搜索</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="main-content" style="height: calc(100vh - 250px);">
      <main-table
        :columns="tableColumns"
        :data="tableList"
        :loading="loading"
        wrapperHeight="calc(100% - 10px)" height="100%"
      >
       <el-table-column slot="img" label="图标图片" width="200" align="center">
         <template slot-scope="scope">
           <img class="task-icon-img" :src="scope.row.img" alt="">
         </template>
       </el-table-column>
       <el-table-column slot="status" label="状态">
         <template slot-scope="scope">
           <el-tag v-if="scope.row.status == '1'" type="success">正常</el-tag>
           <el-tag v-else type="danger">禁用</el-tag>
         </template>
       </el-table-column>
       <el-table-column slot="action" fixed="right" label="操作" width="200">
         <template slot-scope="scope">
           <el-button size="mini" type="success" @click="edit(scope.row)">编辑/查看</el-button>
           <el-button size="mini" type="danger" @click="cancelOrder(scope.row)">删除</el-button>
         </template>
       </el-table-column>

      </main-table>
    </div>

    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      :close-on-click-modal="false"
      width="600px">
      <task-icon-add @cancle="dialogVisible = false" :typeList="typeList" :data="ruleForm" @load="handleSearch"></task-icon-add>
    </el-dialog>
  </div>
</template>
<script>
  import mainTable from "@/components/main-table/index";
  import taskIconAdd from './components/task-icon-add'
  import * as APITask from '@/api/task'
  export default {
    name: "doorserve-serve-taskicon",
    components: {
      mainTable,
      taskIconAdd
    },
    data() {
      return {
        loading: false,
        title: '添加图标',
        dialogVisible: false,
        ruleForm: {},
        currentSelectedData: [],
        pageData: {},
        selectForm: {},
        tableList:[],
        pagination: {
          current: 1,
          size: 15,
          total: 0
        },
        tableColumns: [
          // { prop: "sex", label: "性别", formatter: this.genderFormatter },
          {
            slot: 'img'
          },
          {
            prop: 'name',
            label: '图标名称'
          },
          {
            prop: 'type',
            label: '任务类型',
            formatter: this.typeFormatter
          },
          {
            slot: 'status'
          },
          {
            slot: 'action'
          }
        ],
        key: Math.random(),
        departTypeList: [],
        roleList: [],
        typeList:[]
      };
    },
    created() {},
    beforeDestroy() {},
    mounted() {
      this.gettypeList();
      this.fetchTableList();
    },
    methods: {
      gettypeList () {
        APITask.taskTypeList(this.searchForm).then((res) => {
          const { list = [] } = res.data;
          this.typeList = list || [];
        })
      },
      typeFormatter (row, column, cellValue) {
        for (let i = 0; i < this.typeList.length; i++) {
          if (this.typeList[i].id == cellValue) {
            return this.typeList[i].name
          }
        }
      },
      handleSearch() {
        this.fetchTableList()
      },
      fetchTableList() {
         this.loading = true;
        let data = this.selectForm;
        APITask.tasktypeIconlist(data).then((resp) => {
          if (resp.code === 200) {
            const { list = [], total = 0 } = resp.data;
            this.tableList = list || []
            this.pagination.total = total;
          }
        }).finally(() => {
          this.loading = false;
        });
      },
      cancel(formName) {
        this.$refs[formName].resetFields();
        this[formName] = {};
        this.dialogVisible = false;
      },
      edit (row) {
        this.title = '编辑图标';
        this.key = Math.random();
        this.dialogVisible = true
        this.ruleForm = { ...row }
      },
      handleAdd() {
        this.title = '新增图标';
        this.dialogVisible = true;
        this.key = Math.random();
        this.ruleForm = {
          status: '1',
          img:''
        };
      },
      handleCurrentChange(val) {
        this.selectForm.page = val;
        this.fetchTableList();
      },
      cancelOrder(row) {
        this.$confirm("确定删除该条数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            APITask.tasktypeIcondelete({ id: row.id }).then((resp) => {
              if (resp.code === 200) {
                this.$message({ type: "success", message: "删除成功!"});
                this.fetchTableList();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消操作",
            });
          });
      },
    },
  };
</script>
<style lang="scss" scoped>
  .index-container {
    box-sizing: border-box;
    width: 100%;
    padding: 0 30px !important;
    margin: 0 !important;
    background: #fff !important;

    .main-header {
      padding-top: 4px;
      margin-bottom: 24px;

      .search {
        display: inline-block;

        .search-title {
          flex: 0 0 48px;
          width: 48px;
          font-size: 16px;
          line-height: 36px;
          color: #333;
        }
      }

      .search > div {
        display: inline-block;
        margin-left: 20px;
      }
    }
    .task-icon-img{
      width: 80px;
      height: 80px;
    }
  }

  .form-footer {
    padding-left: 100px;
  }

  .dialog-main {
    .table-do {
      margin-bottom: 15px;
    }

    ::v-deep {
      th {
        .cell {
          color: #fff;
        }
      }
    }
  }
</style>
